package com.tuanzi.loan.business.repository.settlementsystem.impl;

import com.tuanzi.loan.business.repository.settlementsystem.custom.SettlementIncomeActivityRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;

import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanSettlementIncomeActivity.LOAN_SETTLEMENT_INCOME_ACTIVITY;
import static com.tuanzi.loan.generated.jooq.stat.tables.StatActivityJoin.STAT_ACTIVITY_JOIN;

/**
 * @author caiwanlin
 * @create 2018-08-20 20:23
 */
public class SettlementIncomeActivityRepositoryImpl extends JOOQBaseRepositoryPlus implements SettlementIncomeActivityRepositoryCustom{
    @Override
    protected TableImpl getTable() {
        return LOAN_SETTLEMENT_INCOME_ACTIVITY;
    }

    @Override
    public Integer getUv(Date startTime, Date endTime, List<Long> activityIdList) {
        Integer uv = dslContext.select(DSL.sum(STAT_ACTIVITY_JOIN.CLICK_UV).as("uv"))
                .from(STAT_ACTIVITY_JOIN)
                .where(STAT_ACTIVITY_JOIN.DATE.between(new java.sql.Date(startTime.getTime()), new java.sql.Date(endTime.getTime()))
                        .and(STAT_ACTIVITY_JOIN.ACTIVITY_ID.in(activityIdList))).and(STAT_ACTIVITY_JOIN.SPACE_ID.eq(-1000))
                .fetchOne().get("uv",Integer.class);
        return uv;
    }
}
